#if SERVER
using ActorSystem;
using Net;
using Net.AOI;
using Net.Common;
using Net.Share;
using Net.System;

namespace Example2
{
    public partial class Player : Actor
    {
        internal Scene scene;
        internal Client client;
        internal ListSafe<Operation> selfOpers = new();//只发给自己 -- 只有场景线程执行

        public override void OnEnter(IGridObject body)
        {
            if (body is Player player)
            {
                AddOperation(new Operation(OperCommand.EnterArea, player.Identity, player.transform.position, player.transform.rotation)
                {
                    actorType = ActorType,

                });
            }
            else if (body is AIMonster monster)
            {
                monster.roleInCount++;
                AddOperation(new Operation(OperCommand.EnterArea, monster.Identity, monster.transform.position, monster.transform.rotation)
                {
                    actorType = ActorType,
                    direction = monster.destination, //怪物下一个位置, 客户端收到后会自动寻路到目的地
                });
            }
        }

        public override void OnExit(IGridObject body)
        {
            AddOperation(new Operation(Command.ExitArea, body.Identity));
            if (body is AIMonster monster)
                monster.roleInCount++;
        }

        public override void OnUpdate()
        {
            base.OnUpdate();
            if (Grid == null)
                return;
            foreach (var body in Grid)
            {
                var actor = body as Actor;
                if (actor.frame != scene.Frame) //每帧只需要赋值一次
                {
                    actor.tempOpers = actor.currOpers.GetRemoveRange(0, actor.currOpers.Count);
                    actor.frame = scene.Frame;
                }
                currOpers.AddRange(actor.tempOpers);
                currOpers.Add(new Operation(Command.SyncPosition, actor.Identity, actor.transform.Position, actor.transform.Rotation));
            }
            scene.SendOperitions(client, currOpers);
        }

        [OperCmd(Command.Fire)]
        public void OnFire(in Operation opt)
        {
            var bullet = new Bullet();
            bullet.transform.SetPositionAndRotation(opt.position, opt.rotation);
            Scene.AddActor(bullet);
            Scene.RemoveActor(bullet, 5f);
        }
    }
}
#endif